var stopStock = stopStock || {};
var stockVue,clickarray=[2,2,1];
var stockGlobal = {};
stockGlobal.urlParam = getParaByName('type'); //获取当前tab
stockGlobal.pageId = 'pdt_pms_269_rise';
stockGlobal.url_ver = {'url_ver':'SJCGBS-14521'};
stockGlobal.switchTab = stockGlobal.urlParam;  //记录当前tab
stockGlobal.zhibiao = 'zhangfu'; //排序的指标
stockGlobal.sort = 'desc'; // 排序的方式
stockGlobal.tabOne = 0; // 用于tab下拉到底部埋点统计
stockGlobal.tabTwo = 0;
stockGlobal.tabThree = 0;

stopStock = (function(){
	stopStock.initAjax = function(){
		$.ajax({
			type:'get',
			url:hxmConf.hxmAjaxUrl,
			data:{
				'op':'getTodayLimitUpStockList'
			},
			dataType:'json',
			cache:false,
			timeout:10000,
		})
		.fail(function(){
			$('.load-box').addClass('hide');
			$('.load-error').removeClass('hide');
		})
		.done(function(data){
			if(data.code == 0 && !$.isEmptyObject(data.data)){
				$('.load-box').addClass('hide');
				stockGlobal.urlData = data.data;
				stopStock.setLimitStockVue();
			}else{

			}
		})
	},
	stopStock.setLimitStockVue = function(){
		stockVue = new Vue({
			el:'.vueBox',
			data:{
				stockData : stockGlobal.urlData,
				stockList:[],
				stockListBoard:stockGlobal.urlData.wordBoard,
				stockListTouch:stockGlobal.urlData.touchRise,
				stockListRise:stockGlobal.urlData.slowRise,
				roseData:0,
				highData:0,
				codeData:1,
			},
			methods:{
				switchTabTitle:function(type){ //点击表头三个板块
					stockGlobal.urlParam = type;
					hxmClickStat(stockGlobal.pageId+'.'+stockGlobal.switchTab+'.'+type,stockGlobal.url_ver);
					stockGlobal.switchTab = type;
					setTabTitle();
				},
				clickTabSort:function(event){
					var obj = event.currentTarget;
					var id = $(obj).attr('flag-id');
					switch (id) {  //  1  表示正  0  表示逆  2不变
						case "1": //涨停基因
							this.roseData = (++this.roseData)%2;
							this.highData = 0;
							this.codeData = 0;
							addInsBackground(obj,this.roseData);
							stockGlobal.zhibiao = 'jiyin';
							clickarray = [this.roseData,2,2];
							this.roseData == 1 ? (stockGlobal.sort = 'desc') : (stockGlobal.sort = 'asc');
							break;
						case "2": //价格
							this.highData = (++this.highData)%2;
							this.roseData = 0;
							this.codeData = 0;
							addInsBackground(obj,this.highData);
							stockGlobal.zhibiao = 'jiage';
							clickarray = [2,this.highData,2];
							this.highData == 1 ? (stockGlobal.sort = 'desc') : (stockGlobal.sort = 'asc');
							break;
						case "3": // 涨幅
							this.codeData = (++this.codeData)%2;
							this.roseData = 0;
							this.highData = 0;
							addInsBackground(obj,this.codeData);
							stockGlobal.zhibiao = 'zhangfu';
							clickarray = [2,2,this.codeData];
							this.codeData == 1 ? (stockGlobal.sort = 'desc') : (stockGlobal.sort = 'asc');
							break;		
					}
					getAjaxProduct(stockGlobal.urlParam,stockVue.stockList);
					hxmClickStat(stockGlobal.pageId+'.'+stockGlobal.urlParam+'.'+stockGlobal.zhibiao+'.'+stockGlobal.sort,stockGlobal.url_ver);
				},
				JumpToFenShi:function(code,marketid,key){  //点击股票列表
					if(!code || !marketid){
						return false;
					}
					hxmJumpNativeStat(stockGlobal.pageId+'.'+stockGlobal.urlParam+'.'+stockGlobal.zhibiao+'.'+stockGlobal.sort+'.'+key,'2205',{'url_ver':'SJCGBS-14521','targid':'stock_'+code});
					window.location.href = 'client.html?action=ymtz^stockcode='+code+'^marketid='+marketid+'^webid=2205';
				},
				refreshData:function(flag){ //刷新按钮数据
					$.ajax({
						type:'get',
						url:hxmConf.hxmAjaxUrl,
						data:{
							'op':'getTodayLimitUpStockList'
						},
						dataType:'json',
						cache:false,
						timeout:10000,
					})
					.done(function(data){
						if(data.code == 0 && !$.isEmptyObject(data.data)){
							stockGlobal.urlData = data.data;
							setTabTitle();
							if(flag == 'fresh'){
								hxmClickStat(stockGlobal.pageId+'.'+stockGlobal.urlParam+'.refresh',stockGlobal.url_ver);
							}
		
						}
					})
				}
			}
		});
		Vue.nextTick(function(){
			//页面打开埋点
			hxmPageStat({'id':stockGlobal.pageId,'url_ver':'SJCGBS-14521'});
			//滚动到底部埋点
			bottomStat();
			setTabTitle();
		});

	}

	return {
		initAjax : stopStock.initAjax,
		setLimitStockVue : stopStock.setLimitStockVue
	} 
})();


function setTabTitle(){
	$('.rise_title>div span').hide();
	switch (stockGlobal.urlParam){
		case 'slow': //缓慢涨停
			$('.rise_title>div:nth-child(1) span').show();
			stockVue.stockList = stockVue.stockListRise;
			break;
		case 'one': //一字板
			$('.rise_title>div:nth-child(2) span').show();
			stockVue.stockList = stockVue.stockListBoard;
			break;
		case 'touch': // 触及涨停
			$('.rise_title>div:nth-child(3) span').show();
			stockVue.stockList = stockVue.stockListTouch;
			break;		
	}
	getAjaxProduct(stockGlobal.urlParam,stockVue.stockList);
}


function addInsBackground(obj,num){
	$(obj).parent().find('ins').removeClass('topup');
	$(obj).parent().find('ins').removeClass('topdown');
	$(obj).parent().find('ins').removeClass('bottomup');
	$(obj).parent().find('ins').removeClass('bottomdown');
	if(num == 0){
		$(obj).find('ins:first').addClass('topup');
	}else{
		$(obj).find('ins:last').addClass('bottomup');
	}
}


// 滑动到底部埋点
function bottomStat(){
    $(window).on('scroll', function(){
        var dpr = $('html').data('dpr') ? $('html').data('dpr') : 1; // 页面dpr
        var windowHeight = getPlatform() == 'iphone' ? window.screen.height : $(window).height();//分辨率的高度
        var scrollHeight = $(window).scrollTop() / dpr;//document.documentElement.scrollTop;//滚动的高度
        var totalHeight = $(document).height() / dpr;//文档的总高度
        if((scrollHeight) >= (totalHeight - windowHeight)){
           	switch(stockGlobal.urlParam){
           		case 'slow':
           			if(stockGlobal.tabOne == 0){
           				stockGlobal.tabOne = 1;
           				hxmClickStat(stockGlobal.pageId+'.slow.bottom',stockGlobal.url_ver);
           			}
           			break;
           		case 'one':
           			if(stockGlobal.tabTwo == 0){
           				stockGlobal.tabTwo = 1;
           				hxmClickStat(stockGlobal.pageId+'.one.bottom',stockGlobal.url_ver);
           			}
           			break;
           		case 'touch':
           			if(stockGlobal.tabThree == 0){
           				stockGlobal.tabThree = 1;
           				hxmClickStat(stockGlobal.pageId+'.touch.bottom',stockGlobal.url_ver);
           			}
           			break;		
           	}
        }
    });
}


;(function(){
	$('.load-box').removeClass('hide');
	$('body').css('background-color','#f5f5f5');
	$('.code>span>ins:last-child').addClass('bottomup'); //默认设置涨幅开关
	$('.cover').on('click',function(){
		$('.load-error').addClass('hide');
		$('.load-box').removeClass('hide');
		stopStock.initAjax();
	});

	stopStock.initAjax();
})();